#include "SinaApi.h"

QNetworkAccessManager g_netManager;
CSuperConfig g_config;
stDesktop g_desktop;

CSuperApi* CreateApi( stSuperApi* pSuperApi )
{
	CSuperApi* pApi=0;
	switch(pSuperApi->type){
		case SinaApi:
			pApi=new CSinaApi(pSuperApi);
			break;
		case TencentApi:
			pApi=new CSinaApi(pSuperApi);
			break;
		default:
			pApi=new CSinaApi(pSuperApi);
			break;
	}
	if(g_config.InsertApi(pApi))
		return pApi;
	else{
		DeleteApi(pApi);
		return 0;
	}
}

bool DeleteApi( CSuperApi* pApi )
{
	stSuperApi* pSuperApi=pApi->GetApiData();
	switch(pSuperApi->type){
		case SinaApi:
			((CSinaApi*)pApi)->deleteLater();
			break;
		case TencentApi:
			break;
		default:
			break;
	}
	g_config.RemoveApi(pApi);
	return true;
}

bool GetSourceByUrl( QUrl& url, QByteArray &baRet )
{
	QNetworkRequest request;
	request.setUrl(url);
//	request.setRawHeader("User-Agent", ("Mozilla/5.0 (Windows NT 5.1) AppleWebKit/535.1 (KHTML, like Gecko) Chrome/14.0.803.0 Safari/535.1"));

	QNetworkReply *reply = g_netManager.get(request);
	if(reply)
	{
		QEventLoop loop;
		QObject::connect(reply, SIGNAL(finished()), &loop, SLOT(quit()));
		loop.exec();

		QVariant redirectionTarget=reply->attribute(QNetworkRequest::RedirectionTargetAttribute);
		/*
		if (reply->error()) {
					return false;
				}
				else */
		if (!redirectionTarget.isNull()) {        
			QUrl newUrl = request.url().resolved(redirectionTarget.toUrl());
			reply->deleteLater();
			return GetSourceByUrl(newUrl,baRet);
		}
		else{
			baRet = reply->readAll();
			reply->deleteLater();
			return true;
		}
	}
	return false;
}

bool GetSourceByReq( QNetworkRequest& req, QByteArray &baRet )
{
//	request.setRawHeader("User-Agent", ("Mozilla/5.0 (Windows NT 5.1) AppleWebKit/535.1 (KHTML, like Gecko) Chrome/14.0.803.0 Safari/535.1"));

	QNetworkReply *reply = g_netManager.get(req);
	if(reply)
	{
		QEventLoop loop;
		QObject::connect(reply, SIGNAL(finished()), &loop, SLOT(quit()));
		loop.exec();

		QVariant redirectionTarget=reply->attribute(QNetworkRequest::RedirectionTargetAttribute);
		/*
		if (reply->error()) {
					return false;
				}
				else */
		if (!redirectionTarget.isNull()) {        
			QUrl newUrl = req.url().resolved(redirectionTarget.toUrl());
			reply->deleteLater();
			return GetSourceByUrl(newUrl,baRet);
		}
		else{
			baRet = reply->readAll();
			reply->deleteLater();
			return true;
		}
	}
	return false;
}

bool PostSourceByReq( QNetworkRequest& req, QByteArray& baPost,QByteArray &baRet )
{
	req.setHeader(QNetworkRequest::ContentTypeHeader,"application/x-www-form-urlencoded");
	req.setHeader(QNetworkRequest::ContentLengthHeader,baPost.length());
	QNetworkReply *reply = g_netManager.post(req,baPost);
	if(reply)
	{
		QEventLoop loop;
		QObject::connect(reply, SIGNAL(finished()), &loop, SLOT(quit()));
		loop.exec();

		QVariant redirectionTarget=reply->attribute(QNetworkRequest::RedirectionTargetAttribute);
		/*
		if (reply->error()) {
					return false;
				}
				else */
		if (!redirectionTarget.isNull()) {        
			QUrl newUrl = req.url().resolved(redirectionTarget.toUrl());
			reply->deleteLater();
			return GetSourceByUrl(newUrl,baRet);
		}
		else{
			baRet = reply->readAll();
			reply->deleteLater();
			return true;
		}
	}
	return false;
}
